package com.yhh.myUtils;


import com.yhh.Entity.Command;
import com.yhh.Exceptions.ExitException;
import com.yhh.Exceptions.InstructionException;
import com.yhh.Exceptions.NonInstructionException;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class CommandUtils {
    private static DecimalFormat df = null;
    private static CommandUtils commandUtils;

    private CommandUtils() {
        HashSet<String> keyWordSet = new HashSet<>();
        keyWordSet.add("send");
        keyWordSet.add("recv");
        keyWordSet.add("save");
        keyWordSet.add("exit");
        keyWordSet.add("show");
        keyWordSet.add("help");
        keyWordSet.add("clear");
    }

    public static CommandUtils getCommandUtils() {
        if (commandUtils == null) commandUtils = new CommandUtils();
        return commandUtils;
    }

    static {

        // 设置数字格式，保留一位有效小数
        df = new DecimalFormat("#0.0");
        df.setRoundingMode(RoundingMode.HALF_UP);
        df.setMinimumFractionDigits(1);
        df.setMaximumFractionDigits(1);
    }

    public static File hasFile(String fileName) throws FileNotFoundException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
        return file;
    }

    public static String[] InstructionParsing(String in) {
        in = in.trim();
        //获取两个空格的索引
        int index = in.indexOf("  ");
        while (index != -1) {
            //字符串拼接
            in = in.substring(0, index).concat(in.substring(index + 1));
            index = in.indexOf("  ");
        }
        String[] args = in.split(" ");
        return args;
    }
    //检查函数参数
    public boolean checkCommandParameters(Map<String, String> args, Command command) throws Exception, ExitException {
        if("exit".equals(args.get("start"))) throw new ExitException();
        return command.check(args);
    }



    public static int findStrIndex(String[] args, String str) {
        if (args.length == 0 || str == null) return -1;
        int i = 0;
        for (; i < args.length; i++) {
            if (str.equals(args[i])) return i;
        }
        return -1;
    }
    //提取参数，不做检查
    public static Map processCommand(String in) throws NonInstructionException {
        if (in == null) throw new NonInstructionException();
        //空格删除
        String[] args = CommandUtils.InstructionParsing(in);

        Map<String, String> commandMap = new HashMap<>();

        commandMap.put("start", args[0]);

        for (String arg : args) {
            if (arg.startsWith("-") && !arg.endsWith("-")) {
                int strIndex = CommandUtils.findStrIndex(args, arg);
                if (strIndex != args.length - 1 && !args[strIndex + 1].startsWith("-")) {
                    commandMap.put(arg, args[strIndex + 1]);
                } else {
                    commandMap.put(arg, arg);
                }
            }
        }

        return commandMap;
    }

    public static String getFormatFileSize(long length) {
        double size = ((double) length) / (1 << 30);
        if (size >= 1) {
            return df.format(size) + "GB";
        }
        size = ((double) length) / (1 << 20);
        if (size >= 1) {
            return df.format(size) + "MB";
        }
        size = ((double) length) / (1 << 10);
        if (size >= 1) {
            return df.format(size) + "KB";
        }
        return length + "B";
    }
}
